package org.itboys.mobile.service.mysql.web.reward;

import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.mobile.entity.mysql.reward.CooChargeReward;
import org.itboys.mongodb.service.BaseMySqlService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2018/1/29
 * 描述：充值配置
 */
@Service
public class CooChargeRewardService extends BaseMySqlService<CooChargeReward>{
    /**
     *
     * @param request
     * @return
     */
    public PageResult<CooChargeReward> selectReward(HttpServletRequest request){
        Map<String,Object> param= ParamUtil.packageMongoExactParam(request,"type");
        return new PageResult<CooChargeReward>(super.select(param),super.count(param));

    }

    /**
     * 新增
     * @param reward
     */
    public void saveReward(CooChargeReward reward){
        Map<String,Object> param = new HashMap<String,Object>();
        if(reward.getId()==0l){

            param.put("memberLevel",reward.getMemberLevel());
            if(LongUtil.isNotZreo(super.count(param))){
                throw new ErrorException("同用户等级配置，只能存在一个。");
            }
            super.insert(reward);
        }else{
            CooChargeReward chargeReward=super.find(reward.getId());
            if(chargeReward.getMemberLevel()!=reward.getMemberLevel()){
                throw new ErrorException("无法改变用户等级");
            }
            super.update(reward);

        }
    }

    /**
     * 查找单个
     * @param rewardId
     * @return
     */
    public CooChargeReward findOne(Long rewardId){
        CooChargeReward reward = null;
        if(LongUtil.isNotZreo(rewardId)){
            reward=super.find(rewardId);
        }else {
            reward=new CooChargeReward();
            reward.setId(rewardId);
        }
        return reward;
    }
}
